import tornado.ioloop
import tornado.iostream
import asyncio
import socket

class TCPClient(object):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.stream = None
        self.ioloop = tornado.ioloop.IOLoop.current()

    async def connect(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        stream = tornado.iostream.IOStream(sock)
        await stream.connect((self.host, self.port))
        self.stream = stream
        return stream

    async def send_messages(self, count):
        stream = await self.connect()
        for i in range(count):
            message = f"Message {i}\n".encode('utf-8')
            await stream.write(message)
            response = await stream.read_until(b'\n')
            
            # print(f"Received: {response.decode('utf-8')}")

    def run(self):
        asyncio.run(self.send_messages(100000))


if __name__ == "__main__":
    client = TCPClient('localhost', 8888)
    client.run()